<script setup>
import { computed } from 'vue'

const props = defineProps({
  total: {
    type: Number,
    required: true
  },
  currentPage: {
    type: Number,
    default: 1
  },
  pageSize: {
    type: Number,
    default: 10
  },
  maxPageButtons: {
    type: Number,
    default: 5
  }
})

const emit = defineEmits(['update:currentPage', 'pageChange'])

const totalPages = computed(() => Math.ceil(props.total / props.pageSize))

const pageNumbers = computed(() => {
  const halfMaxButtons = Math.floor(props.maxPageButtons / 2)
  let startPage = Math.max(props.currentPage - halfMaxButtons, 1)
  let endPage = Math.min(startPage + props.maxPageButtons - 1, totalPages.value)

  if (endPage - startPage + 1 < props.maxPageButtons) {
    startPage = Math.max(endPage - props.maxPageButtons + 1, 1)
  }

  return Array.from({ length: endPage - startPage + 1 }, (_, i) => startPage + i)
})

const handlePageChange = (page) => {
  if (page < 1 || page > totalPages.value || page === props.currentPage) return
  emit('update:currentPage', page)
  emit('pageChange', page)
}
</script>

<template>
  <div class="pagination" v-if="totalPages > 1">
    <button
      class="pagination-btn"
      :disabled="currentPage === 1"
      @click="handlePageChange(currentPage - 1)"
    >
      上一页
    </button>

    <span class="page-info">{{ currentPage }} / {{ totalPages }}</span>

    <button
      class="pagination-btn"
      :disabled="currentPage === totalPages"
      @click="handlePageChange(currentPage + 1)"
    >
      下一页
    </button>

    <select 
      class="page-size-select" 
      :value="pageSize"
      @change="$emit('update:pageSize', parseInt($event.target.value))"
    >
      <option value="10">10条/页</option>
      <option value="20">20条/页</option>
      <option value="50">50条/页</option>
    </select>

    <span v-if="pageNumbers[0] > 2" class="pagination-ellipsis">...</span>

    <button
      v-for="page in pageNumbers"
      :key="page"
      class="pagination-btn"
      :class="{ active: page === currentPage }"
      @click="handlePageChange(page)"
    >
      {{ page }}
    </button>

    <span
      v-if="pageNumbers[pageNumbers.length - 1] < totalPages - 1"
      class="pagination-ellipsis"
    >
      ...
    </span>

    <button
      v-if="pageNumbers[pageNumbers.length - 1] < totalPages"
      class="pagination-btn"
      @click="handlePageChange(totalPages)"
    >
      {{ totalPages }}
    </button>

    <button
      class="pagination-btn"
      :disabled="currentPage === totalPages"
      @click="handlePageChange(currentPage + 1)"
    >
      下一页
    </button>
  </div>
</template>

<style scoped>
.pagination {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pagination-btn {
  padding: 6px 12px;
  border: 1px solid #d9d9d9;
  background: #fff;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination-btn:hover:not(:disabled) {
  color: #1890ff;
  border-color: #1890ff;
}

.pagination-btn:disabled {
  cursor: not-allowed;
  color: #d9d9d9;
}

.pagination-btn.active {
  color: #fff;
  background: #1890ff;
  border-color: #1890ff;
}

.pagination-ellipsis {
  color: #999;
}
</style>